<template>
	<view class="warp">
		<!-- 顶部 start -->
		<!-- #ifdef WEB -->
		<view class="navbar_top">
			<uv-navbar leftText="返回" :fixed="false" title="试卷详情" border :safeAreaInsetTop="false" :placeholder="true" @leftClick="jumpUrl('1','navigateBack')" @rightClick="jumpUrl('/pages/index/index','redirectTo')">
				<template v-slot:left>
					<view class="uv-nav-slot">
						<uv-icon name="arrow-left" size="19"></uv-icon>
					</view>
				</template>
				<template v-slot:right>
					<view class="uv-nav-slot">
						<uv-icon name="home" size="19"></uv-icon>
					</view>
				</template>
			</uv-navbar>
		</view>
		<uv-gap height="44" bgColor="#F1F1F5"></uv-gap>
		<!-- #endif -->
		<!-- #ifdef MP-WEIXIN -->
		<uv-navbar leftText="返回" :fixed="true" title="试卷详情" border :safeAreaInsetTop="true" :placeholder="true" @leftClick="jumpUrl('1','navigateBack')" @rightClick="jumpUrl('/pages/index/index','redirectTo')">
			<template v-slot:left>
				<view class="uv-nav-slot">
					<uv-icon name="arrow-left" size="19"></uv-icon>
				</view>
			</template>
			<template v-slot:right>
				<view class="uv-nav-slot">
					<uv-icon name="home" size="19"></uv-icon>
				</view>
			</template>
		</uv-navbar>
		<!-- #endif -->
		<!-- 顶部 end -->
		<!-- 试卷盒子 start -->
		<view class="test_box" v-if="is_end === 0 || start_retake == 1">
			<view class="test_title">
				{{row.title}}<text class="test_title_score">（总分：{{row.total_score}}）</text>
			</view>
			<view class="test_block" v-for="(v,k) in question" :key="k" v-if="v.question.length > 0">
				<view class="block_title">
					<text>{{v.number}}、</text> {{v.value}} <text class="score_desc">（{{v.total_score_desc}}）</text>
				</view>
				<!-- 题目 start -->
				<view class="block_question">
					<view class="question_item" v-for="(v2,k2) in v.question" :key="k2">
						<view class="question_title" v-if="v.id != 4">
							<text class="text_key">{{k2+1}}.</text>
							<text class="text_title">{{v2.title}}</text>
						</view>


						<!-- 问答题 start -->
						<view class="answer_list" v-if="v.id == 5">
							<uv-textarea v-model="question[k]['question'][k2]['answer_value']" :textStyle="{fontSize:'12px',color:'#18bc9c'}" placeholder="请在此处作答"></uv-textarea>
						</view>
						<!-- 问答题 end -->
						<!-- 单选题 start -->
						<view class="answer_list" v-if="v.id == 1">
							<view class="answer_item" v-for="(v3,k3) in v2.option_arr" :key="k3" @click="click_radio(k,k2,v3)">
								<image class="radio_img" v-show="v2.answer_value !== v3" :src="IMG_URL + 's_radio_0.png'" mode="scaleToFill"></image>
								<image class="radio_img" v-show="v2.answer_value === v3" :src="IMG_URL + 's_radio_1.png'" mode="scaleToFill"></image>
								<text class="answer_key">{{integerToAlphaBase26(k3+1)}}、{{v3}}</text>
							</view>
						</view>
						<!-- 单选题 end -->
						<!-- 多选题 start -->
						<view class="answer_list" v-if="v.id == 2">
							<view class="answer_item" v-for="(v3,k3) in v2.option_arr" :key="k3" @click="click_checkbox(k,k2,v3)">
								<image class="radio_img" v-show="v2.answer_value.indexOf(v3) === -1" :src="IMG_URL + 's_checkbox_0.png'" mode="scaleToFill"></image>
								<image class="radio_img" v-show="v2.answer_value.indexOf(v3) > -1" :src="IMG_URL + 's_checkbox_1.png'" mode="scaleToFill"></image>
								<text class="answer_key">{{integerToAlphaBase26(k3+1)}}、{{v3}}</text>
							</view>
						</view>
						<!-- 多选题 end -->
						<!-- 判断题 start -->
						<view class="pd_answer_list answer_list" v-if="v.id == 3">
							<view class="answer_item" v-for="(v3,k3) in v2.option_arr" :key="k3" @click="click_radio(k,k2,v3)">
								<image class="radio_img" v-show="v2.answer_value !== v3" :src="IMG_URL + 's_radio_0.png'" mode="scaleToFill"></image>
								<image class="radio_img" v-show="v2.answer_value === v3" :src="IMG_URL + 's_radio_1.png'" mode="scaleToFill"></image>
								<view class="answer_key"><uv-icon class="answer_key_icon" v-if="v3=='正确'" name="checkbox-mark" size="22"></uv-icon><uv-icon class="answer_key_icon" v-else name="close" size="22"></uv-icon> {{v3}}</view>
							</view>
						</view>
						<!-- 判断题 end -->
						<!-- 填空题标题 start -->
						<view class="tkt_question_title" v-if="v.id == 4">
							<view class="text_title"><text class="text_key">{{k2+1}}.</text>
								<view class="title_list" v-for="(v3,k3) in v2.title_arr" :key="k3">
									<view v-if="v3 !== '(________)'">{{v3}}</view>
									<view class="tkt_box" v-else>
										<uv-input style="width: 100px;" color="#18bc9c" border="bottom" v-model="question[k]['question'][k2]['answer_value'][k3]"></uv-input>
									</view>
								</view>
							</view>
						</view>
						<!-- 填空题标题 end -->

					</view>
				</view>
				<!-- 题目 end -->
			</view>
			<view class="input_btn" @click="save">提交</view>
			<uv-gap height="10" bgColor="#fff"></uv-gap>
		</view>
		<!-- 试卷盒子 end -->
		<!-- 成绩盒子 start -->
		<view class="test_end" v-if="is_end === 1 && start_retake == 0">
			<view class="img">
				<image :src="IMG_URL + 'test02.png'" mode="aspectFit"></image>
			</view>
			<view class="test_title">
				{{row.title}}<text class="test_title_score">（总分：{{row.total_score}}）</text>
			</view>
			<view class="test_warp">
				<view class="test_card">
					<view class="test_card_i card_i_1">
						<view class="test_weigh">姓名：</view>
						<view class="test_value">{{nickname}}</view>
					</view>
					<view class="test_card_i card_i_2">
						<view class="test_weigh">成绩：</view>
						<view class="test_value" v-if="last_test.status == 1">{{last_test.score}}</view>
						<view class="test_value" v-else style="color:chocolate;">待批阅...</view>
					</view>
					<view class="test_card_i card_i_2" v-if="last_test.status == 1">
						<view class="test_weigh">状态：</view>
						<view class="test_value" style="color:chocolate;" v-if="last_test.is_pass == 0">不及格</view>
						<view class="test_value" v-else>及格</view>
					</view>
					<view class="test_card_i card_i_2">
						<view class="test_weigh">提交时间：</view>
						<view class="test_value">{{customDate("Y-m-d H:i:s",last_test.createtime)}}</view>
					</view>
				</view>
				<view class="test_card_btn" v-if="is_retake == 1" @click="jumpUrl(`/pages/test/details?id=${id}&start_retake=1`,'redirectTo')">
					开始复考
				</view>
			</view>

		</view>
		<!-- 成绩盒子 end -->
		<!-- 错误box start -->
		<view class="error_box" v-if="error_msg !== ''">
			<view class="error_img">
				<uv-empty mode="list" :text="error_msg" textColor="crimson"></uv-empty>
			</view>
		</view>
		<!-- 错误box end -->
		<!-- 底部tabbar start -->
		<bastTabbar :tabbar_index="1000"></bastTabbar>
		<!-- 底部tabbar end -->
		<!-- 填写判断弹窗 start -->
		<uv-modal ref="write_modal" showCancelButton buttonReverse @cancel="$refs.write_modal.close();" @confirm="save_next">
			<view class="slot-content">
				<rich-text :nodes="write_content"></rich-text>
			</view>
		</uv-modal>
		<!-- 填写判断弹窗 end -->
	</view>
</template>

<script>
	import bastTabbar from '@/component/base-tabbar.vue'
	export default {
		components: {
			bastTabbar,
		},
		onLoad(options) {
			this.id = options.id
			this.start_retake = options.start_retake || 0
			this.getRow()
		},
		data() {
			return {
				id: '',
				row: {},
				is_end: '',
				last_test: {},
				nickname: '',
				question: [],
				radiovalue: 0,
				tkt_key: 0,
				is_retake: '',// 复考
				start_retake:0,
				error_msg: '',
				IMG_URL: this.$IMG_URL,
				write_content: `<p style="color:#f75444;font-weight:bold;text-align:center;">是否提交？</p><p style="margin-top:20px;"><span style="color:#1688f1;">已答：[[written]]</span><span style="padding:0 10px;color:#f75444;">/</span><span style="color:#f39c12;">未答：[[unwritten]]</span></p>`
			}
		},
		methods: {
			async getRow() {
				const res = await this.$myRequest({
					url: "test/details",
					type: "post",
					data: {id:this.id},
				});
				if(res.code){
					this.row = res.data.test_data
					this.question = res.data.question
					this.is_end = res.data.is_end
					this.nickname = res.data.nickname
					this.last_test = res.data.last_test
					this.is_retake = res.data.is_retake
				} else {
					this.error_msg = res.msg
					uni.showToast({
						title: res.msg,
						icon: "error"
					})
				}
			},
			radio_change(e) {
				console.log(e)
			},
			// 单选题
			click_radio(k,k2,item) {
				this.question[k]['question'][k2]['answer_value'] = item
			},
			// 多选题
			click_checkbox(k,k2,item) {
				const index = this.question[k]['question'][k2]['answer_value'].indexOf(item);
				if (index > -1) {
					this.question[k]['question'][k2]['answer_value'].splice(index, 1); // 删除已存在的元素
				} else {
					this.question[k]['question'][k2]['answer_value'].push(item); // 加入元素
				}
			},
			async save() {
				let unwritten = 0;
				let written = 0;
				this.question.forEach((v,k)=>{
					// console.log(v)
					v.question.forEach((v2,k2)=>{
						// console.log(v2)
						if([1,3,5].includes(v2.type) && v2.answer_value.trim() === ""){
							unwritten++
						} else if( [2,4].includes(v2.type) && v2.answer_value.every(value => value === "") ){
							unwritten++
						} else {
							written++
						}

					})
				})
				if(written === 0){
					uni.showToast({
						icon:"error",
						title:"不允许交白卷"
					})
					return false
				}
				if(unwritten > 0){
					let write_content = this.write_content
					write_content = write_content.replace("[[written]]",written)
					write_content = write_content.replace("[[unwritten]]",unwritten)
					this.write_content = write_content
					this.$refs.write_modal.open();
				} else {
					this.save_next()
				}
			},
			async save_next() {
				const res = await this.$myRequest({
					url: "test/save?token="+uni.getStorageSync("userInfo").token,
					type: "post",
					data: {test_id:this.id,question:this.question,start_retake:this.start_retake},
					header: {"Content-Type": "application/json;charset=UTF-8"}
				});
				if(res.code){
					this.jumpUrl(`/pages/test/details?id=${this.id}`,'redirectTo')
				} else {
					uni.showToast({
						title: res.msg,
						icon: "error"
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	.warp{
		background-color: #FFF;
		min-height: 100vh;
		max-width: 1600rpx;
		margin: auto;
		color: #333;
		.navbar_top{
			z-index: 1000;background-color: red;position: relative;
			.uv-navbar{
				position: fixed;
				width: 100%;
				max-width: 1600rpx;
			}
		}

		// 试卷盒子
		.test_box{
			margin: 20rpx 0;
			.test_title{
				text-align: center;
				padding: 0 20rpx;
				.test_title_score{
					font-size: 26rpx;
					color: #f75444;
				}
			}
			.test_block{
				padding: 20rpx;
				font-size: 28rpx;
				.block_title{
					font-size: 30rpx;
					color: #1688f1;
					font-weight: bold;
					.score_desc{
						font-size: 24rpx;
						color: crimson;
					}
				}
				// 题目 start
				.block_question{
					margin-left: 1em;
					.question_item{
						margin-top: 40rpx;
						.question_title{
							.text_key{
								margin-right: 20rpx;
								font-weight: bold;
							}
							.text_title{}
						}
						// 填空题标题样式
						.tkt_question_title{
							display: flex;
							flex-wrap: wrap;
							.text_key{
								display: inline;
								margin-right: 20rpx;
								font-weight: bold;
							}
							.text_title{
								display: flex;
								flex-wrap: wrap;
								align-items: center;
								.title_list{
									// display: flex;
									// line-height: 2em;
									.tkt_box{
										margin: 0 10rpx;
									}
								}
							}
						}
						.answer_list{
							display: flex;
							flex-wrap: wrap;
							margin-top: 20rpx;
							margin-left: 1em;
							.answer_item{
								margin: 10rpx 0;
								box-sizing: border-box;
								margin-right: 100rpx;
								display: flex;
								align-items: center;
								cursor: pointer;
								.answer_key{
									font-weight: bold;
									margin-right: 14rpx;
									display: flex;
									align-items: center;
									.answer_key_icon{
										margin-right: 8rpx;
									}
								}
								.radio_img{
									width: 34rpx;
									height: 34rpx;
									margin-right: 10rpx;
								}
							}
						}
						.pd_answer_list{
							display: block;
						}
					}
				}
				// 题目 end
			}
			.input_btn{
				margin: 20rpx auto;
				height: 80rpx;
				line-height: 80rpx;
				width: 95%;
				text-align: center;
				font-size: 28rpx;
				border-radius: 8rpx;
				color: #fff;
				background-color: #1688f1;
			}
		}
		.test_success{
			width: 100vw;
			text-align: center;
		}
		.test_end{
			width: 100%;
			text-align: center;
			.img {
				image{
					margin-top: 40rpx;
					width: 300rpx;
					height: 300rpx;
				}
			}
			.test_msg{
				margin-top: 40rpx;font-size: 30rpx;text-align: center;color: #1688f1;
			}
			.test_title{
				margin-top: 40rpx;font-size: 30rpx;padding: 0 20rpx;
				.test_title_score{
					font-size: 26rpx;
					color: #f75444;
				}
			}
			.test_warp{
				margin: 80rpx 0;
				.test_card{
					margin: auto;
					border: 1px solid #333;
					width: 80%;
					max-width: 600rpx;
					border-radius: 4rpx;
					font-size: 28rpx;

					.test_card_i{
						text-align: left;
						display: flex;
						align-items: inherit;
						margin: 20rpx 0;
						padding: 0 20rpx;
						.test_weigh{
							font-weight: bold;
							color: #333;
							width: 168rpx;
							text-align: right;
						}
						.test_value{
							color: crimson;
							font-size: 30rpx;
						}
					}
					.card_i_2{
						border-top: 1px solid #333;
						padding-top: 20rpx;
					}
				}
				.test_card_btn{
					margin: 80rpx auto 0;
					width: 100%;
					max-width: 600rpx;
					height: 80rpx;
					line-height: 80rpx;
					text-align: center;
					background-color: #1688f1;
					color: #FFF;
					border-radius: 4rpx;
					font-size: 30rpx;
				}
			}

		}
		.error_box{
			.error_img{
				margin: 140rpx auto 0;
			}
			.error_text{
				margin-top: 80rpx;
				text-align: center;
				font-size: 30rpx;
				color: crimson;
			}
		}
	}
</style>